
package cn.com.quickpay.core.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import cn.com.quickpay.core.comm.IRequestService;
import cn.com.quickpay.core.common.Configure;
import cn.com.quickpay.core.util.JsonUtils;
import cn.com.quickpay.core.util.MD5Utils;

public abstract class IPostService {

	/**
	 * 通讯接口，返回报文为json格式
	 *
	 * @param params
	 *            post待发送的包含所有参数的Map
	 * @return JSON报文
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws MalformedURLException
	 * @throws KeyManagementException
	 */
	protected static String txn(String url, Map<String, String> params) {
		String jsonStr = null;
		try {
			IRequestService requestor = IRequestService.getInstance();
			jsonStr = (String) requestor.sendPost(url, params);
			if (Configure.isNeedChkSign()) {
				Map<String,String> signMap = JsonUtils.jsonToMap(jsonStr);
				if (signMap.containsKey("sign")&& !MD5Utils.verify(signMap)) {
					jsonStr = Configure.SIGN_ERROR_RESULT;
				}
			}
		} catch (Exception e) {
			jsonStr = Configure.SYS_ERROR_RESULT;
		}
		return jsonStr;
	}

	/**
	 * 外层调用接口，只传入需要用户输入的参数，并补全所有参数
	 *
	 * @param params
	 * @return JSON报文
	 */
	public abstract String exec(Map<String, String> params);
}
